package com.frank.oj.controller.admin;


import cn.hutool.json.JSONObject;
import com.frank.oj.common.result.CommonResult;
import com.frank.oj.model.dto.DBAndRedisConfigDTO;
import com.frank.oj.model.dto.EmailConfigDTO;
import com.frank.oj.model.dto.TestEmailDTO;
import com.frank.oj.model.dto.WebConfigDTO;
import com.frank.oj.service.admin.system.ConfigService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author frank
 * @Date 2024/3/27
 */
@RestController
@RequestMapping("/api/admin/config")
public class ConfigController {

    @Resource
    private ConfigService configService;

    /**
     * 获取前台系统相关信息，cpu、内存···
     * @return 系统信息
     */
    @RequiresRoles(value = {"root", "admin", "problem_admin"}, logical = Logical.OR)
    @RequestMapping("/get-service-info")
    public CommonResult<JSONObject> getServiceInfo() {
        return configService.getServiceInfo();
    }

    /**
     * 获取判题机相关信息
     * @return 判题机相关信息
     */
    @RequiresRoles(value = {"root", "admin", "problem_admin"}, logical = Logical.OR)
    @RequestMapping("/get-judge-service-info")
    public CommonResult<List<JSONObject>> getJudgeServiceInfo() {
        return configService.getJudgeServiceInfo();
    }

    /**
     * 网站基本信息
     */
    @RequiresPermissions("system_info_admin")
    @RequestMapping("/get-web-config")
    public CommonResult<WebConfigDTO> getWebConfig() {
        return CommonResult.successResponse(configService.getWebConfig());
    }
    @RequiresPermissions("system_info_admin")
    @PutMapping("/set-web-config")
    public CommonResult<Void> setWebConfig(@RequestBody WebConfigDTO config) {

        try {
            configService.setWebConfig(config);
            return CommonResult.successResponse();
        } catch (Exception e) {
            return CommonResult.errorResponse(e.getMessage());
        }
    }

    /**
     * 邮箱信息
     */
    @RequiresPermissions("system_info_admin")
    @RequestMapping("/get-email-config")
    public CommonResult<EmailConfigDTO> getEmailConfig() {

        return CommonResult.successResponse(configService.getEmailConfig());
    }
    @RequiresPermissions("system_info_admin")
    @PutMapping("/set-email-config")
    public CommonResult<Void> setEmailConfig(@RequestBody EmailConfigDTO config) {
        try {
            configService.setEmailConfig(config);
            return CommonResult.successResponse();
        } catch (Exception e) {
            return CommonResult.errorResponse(e.getMessage());
        }
    }
    /**
     * 数据库
     */
    @RequiresPermissions("system_info_admin")
    @RequestMapping("/get-db-and-redis-config")
    public CommonResult<DBAndRedisConfigDTO> getDBAndRedisConfig() {
        return CommonResult.successResponse(configService.getDBAndRedisConfig());
    }
    @RequiresPermissions("system_info_admin")
    @PutMapping("/set-db-and-redis-config")
    public CommonResult<Void> setDBAndRedisConfig(@RequestBody DBAndRedisConfigDTO config) {
        try {
            configService.setDBAndRedisConfig(config);
            return CommonResult.successResponse();
        } catch (Exception e) {
            return CommonResult.errorResponse(e.getMessage());
        }
    }

    /**
     * 删除轮播图
     */
    @RequiresPermissions("system_info_admin")
    @DeleteMapping("/home-carousel")
    public CommonResult<Void> deleteHomeCarousel(@RequestParam("id") Long id) {

        try {
            configService.deleteHomeCarousel(id);
            return CommonResult.successResponse();
        } catch (Exception e) {
            return CommonResult.errorResponse(e.getMessage());
        }
    }

    /**
     * 测试邮箱
     */
    @RequiresPermissions("system_info_admin")
    @PostMapping("/test-email")
    public CommonResult<Void> testEmail(@RequestBody TestEmailDTO testEmailDto) {
        return configService.testEmail(testEmailDto);
    }
}
